package com.leyou.service;

import com.leyou.common.utils.CodecUtils;
import com.leyou.common.utils.NumberUtils;
import com.leyou.mapper.UserMapper;
import com.leyou.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //验证码与其他通知短信的前缀码区分
    private static final String KEY_PREFIX = "user:verify";

    /**
     * 检查数据
     *
     * @param data 数据
     * @param type 类型
     * @return {@link Boolean}
     */
    public Boolean checkData(String data, Integer type) {
        User auser = new User();
        if(type == 1){
            auser.setUsername(data);
        }else if(type == 2){
            auser.setPhone(data);
        }else {
            return null;//参数不合法
        }
        return this.userMapper.selectCount(auser) == 0;//检查数据是否已经存在
    }

    /**
     * 发送验证代码
     *
     * @param phone 电话
     */
    public void sendVerifyCode(String phone) {
        if (StringUtils.isBlank(phone)){
            return;//空
        }
        //生成验证码
        String code = NumberUtils.generateCode(6);//6位数验证码
        //放在RABBITMQ管道
        Map<String,String> msg =new HashMap<>();
        msg.put("phone",phone);
        msg.put("code",code);
        this.amqpTemplate.convertAndSend("leyou.sms.exchange","sms.verify.code",msg);
        //存到REDIS的服务器
        this.stringRedisTemplate.opsForValue().set(KEY_PREFIX+phone,code,5, TimeUnit.HOURS);


    }
    /**
     * 注册
     *
     * @param user 用户
     * @param code 验证码码
     * @return {@link ResponseEntity}<{@link Void}>
     */
    public void register(User user, String code) {
        String cachecode = this.stringRedisTemplate.opsForValue().get(KEY_PREFIX + user.getPhone());
        //检验验证码
        if (!StringUtils.equals(cachecode,code)){
            return;
        }
        //加盐加密
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);
        //用生成的盐对用户初始的密码进行加密存入数据库
        user.setPassword(CodecUtils.md5Hex(user.getPassword(),salt));
        //新增用户
        user.setId(null);
        user.setCreated(new Date());
        this.userMapper.insertSelective(user);

    }
    /**
     * 查询用户
     *
     * @param username 用户名
     * @param password 密码
     * @return {@link ResponseEntity}<{@link User}>
     */
    public User queryUser(String username, String password) {
        User one = new User();
        one.setUsername(username);
        //查询用户
        User user = this.userMapper.selectOne(one);

        if (user == null){
            return null;//未注册用户
        }
        //密码加盐加密,输入的密码用已经获取到的盐加密
        String md5Hex = CodecUtils.md5Hex(password, user.getSalt());
       /* System.out.println(""+md5Hex);
        System.out.println(""+user.getPassword());*/
        //验证
        if (StringUtils.equals(md5Hex,user.getPassword())){
            return user;//正确
        }
        return null;//密码不正确
    }
}
